﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace Sys
{
    public class EmailUtils
    {
        public static bool Sendmail(string accountname, string password, string SmtpHost, int SmtpPort, string EmailAddress, string CompanyName, string Title, string Content, System.Collections.Generic.List<string> att, string CC, string BCC)
        {
            bool result;
            SmtpClient smtpClient = new SmtpClient(SmtpHost, SmtpPort);
            //using (SmtpClient smtpClient = new SmtpClient(SmtpHost, SmtpPort))
            {
                using (MailMessage mailMessage = new MailMessage())
                {
                    mailMessage.To.Add(EmailAddress);
                    mailMessage.From = new MailAddress(accountname, CompanyName, System.Text.Encoding.UTF8);
                    if (!CC.Trim().Equals(string.Empty))
                    {
                        string[] array = CC.Split(new char[]
						{
							';'
						});
                        string[] array2 = array;
                        int i = 0;
                        while (i < array2.Length)
                        {
                            string addresses = array2[i];
                            try
                            {
                                mailMessage.CC.Add(addresses);
                            }
                            catch
                            {
                            }
                        IL_8A:
                            i++;
                            continue;
                            goto IL_8A;
                        }
                    }
                    if (!BCC.Trim().Equals(string.Empty))
                    {
                        string[] array3 = BCC.Split(new char[]
						{
							';'
						});
                        string[] array2 = array3;
                        int i = 0;
                        while (i < array2.Length)
                        {
                            string addresses = array2[i];
                            try
                            {
                                mailMessage.Bcc.Add(addresses);
                            }
                            catch
                            {
                            }
                        IL_F9:
                            i++;
                            continue;
                            goto IL_F9;
                        }
                    }
                    mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
                    mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                    //mailMessage.HeadersEncoding = System.Text.Encoding.UTF8;
                    mailMessage.IsBodyHtml = true;
                    mailMessage.Priority = MailPriority.High;
                    mailMessage.Subject = Title;
                    mailMessage.Body = Content;
                    if (att.Count > 0)
                    {
                        foreach (string current in att)
                        {
                            mailMessage.Attachments.Add(new Attachment(current));
                        }
                    }
                    smtpClient.UseDefaultCredentials = false;
                    smtpClient.Credentials = new NetworkCredential(accountname, password);
                    smtpClient.EnableSsl = true;
                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    try
                    {
                        smtpClient.Send(mailMessage);
                    }
                    catch
                    {
                        result = false;
                        return result;
                    }
                    result = true;
                }
            }
            return result;
        }
    }
}
